﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DDD.Core;
using FloatComparer = DDDTest.Helper.FloatComparer;


namespace DDDTest.Core {
    [TestClass]
    public class TestAnimationTrackSampling2 {
        [TestMethod]
        public void Test_Initial_State_Keyframe () {
            var track = new AnimationTrack (3, 1, InterpolationType.Linear);
            track.SetDuration (5);

            var value = new float[1];

            track.Sample (-1, value);
            Assert.AreEqual (0, value[0]);

            track.Sample (0, value);
            Assert.AreEqual (0, value[0]);

            track.Sample (1, value);
            Assert.AreEqual (0, value[0]);
        }

        [TestMethod]
        public void Test_Multi_Component_Keyframe () {
            var track = new AnimationTrack (2, 4, InterpolationType.Linear);
            track.SetKeyframe (0, 1, new float[] { 0, 1, 2, 3 });
            track.SetKeyframe (1, 2, new float[] { 4, 5, 6, 7 });
            track.SetDuration (3);
            track.SetLoopMode (LoopMode.AdditiveLoop);

            var expected = new float[4] { 2, 3, 4, 5 };
            var value = new float[4];
            track.Sample (1.5f, value);
            
            CollectionAssert.AreEqual (expected, value, new FloatComparer (0.0001f));
        }

        [TestMethod]
        public void Test_Single_Keyframe () {
            var track = new AnimationTrack (1, 1, InterpolationType.Linear);
            track.SetKeyframe (0, 1, new float[] { 1 });
            track.SetDuration (2);
            track.SetLoopMode (LoopMode.Loop);

            var value = new float[1];

            track.Sample (-0.5f, value);
            Assert.AreEqual (1, value[0]);

            track.Sample (0.5f, value);
            Assert.AreEqual (1, value[0]);

            track.Sample (1.5f, value);
            Assert.AreEqual (1, value[0]);

            track.Sample (2.5f, value);
            Assert.AreEqual (1, value[0]);
        }

        [TestMethod]
        public void Test_Degenerated_Keyframe () {
            var track = new AnimationTrack (5, 1, InterpolationType.Step);
            track.SetKeyframe (0, 1, new float[] { 1 });
            track.SetKeyframe (1, 2, new float[] { 1 });
            track.SetKeyframe (2, 2, new float[] { 2 });
            track.SetKeyframe (3, 2, new float[] { 3 });
            track.SetKeyframe (4, 3, new float[] { 3 });
            track.SetDuration (5);
            track.SetLoopMode (LoopMode.Single);

            var value = new float[1];

            track.Sample (1.5f, value);
            Assert.AreEqual (1, value[0]);

            track.Sample (2.5f, value);
            Assert.AreEqual (3, value[0]);
        }

        [TestMethod]
        public void Test_Partial_ValidRange_Keyframe () {
            var track = new AnimationTrack (5, 1, InterpolationType.Step);
            track.SetKeyframe (0, 0, new float[] { 0 });
            track.SetKeyframe (1, 1, new float[] { 1 });
            track.SetKeyframe (2, 2, new float[] { 2 });
            track.SetKeyframe (3, 3, new float[] { 3 });
            track.SetKeyframe (4, 4, new float[] { 4 });
            track.SetDuration (4);
            track.SetLoopMode (LoopMode.Single);

            var value = new float[1];

            track.SetValidRange (0, 1);
            track.Sample (1.5f, value);
            Assert.AreEqual (1, value[0]);

            track.SetValidRange (1, 2);
            track.Sample (1.5f, value);
            Assert.AreEqual (1, value[0]);

            track.SetValidRange (2, 3);
            track.Sample (1.5f, value);
            Assert.AreEqual (2, value[0]);

            track.SetValidRange (3, 4);
            track.Sample (1.5f, value);
            Assert.AreEqual (3, value[0]);
        }
    }


}
